using UnityEngine;
using System.Collections;

public class mcMoveObject2D : MonoBehaviour
{
	public float moveSpeed = 0.001f;	
	protected bool mouseDown = false;
	protected float xPosition = 0;
	
	// Use this for initialization
	void Start () 
	{
	}
	// Update is called once per frame
	void Update ()
	{
		this.MoveFromMouse();
		this.MoveFromKeyboard();
	}
	
	void MoveFromMouse()
	{
		if(this.moveSpeed == 0)
			return;
		
		if(Input.GetMouseButton(0) == false)
			return;		
		
		if(Input.GetMouseButtonDown(0) == true)
		{
			Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
			this.xPosition = ray.origin.x;
			
			this.mouseDown = true;
			return;
		}
		else if(Input.GetMouseButtonUp(0) == true)
		{
			this.mouseDown = false;
			return;
		}
		
		float speed = this.moveSpeed;
		Ray newRay = Camera.main.ScreenPointToRay(Input.mousePosition);
		if((newRay.origin.x - this.xPosition) == 0)
			return;
		else if((newRay.origin.x - this.xPosition) > 0)
			speed = -speed;


		this.transform.Translate(speed, 0, 0, this.transform);
		
		this.xPosition = newRay.origin.x;
	}
	
	void MoveFromKeyboard()
	{
		if(Input.GetKey(KeyCode.LeftArrow) == true)
		{
			this.transform.Translate(-Time.deltaTime, 0, 0, Camera.main.transform);
		}
		else if(Input.GetKey(KeyCode.RightArrow) == true)
		{
			this.transform.Translate(Time.deltaTime, 0, 0, Camera.main.transform);
		}
	}
}
